package com.controle;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FlowEvent;

import com.negocio.entidades.CompCurr;
import com.negocio.entidades.FormaParticipacao;
import com.negocio.entidades.TipoCompCurr;
import com.negocio.entidades.Usuario;
import com.negocio.facade.CompCurrRemote;

@SessionScoped
@ManagedBean(name ="compCurrMB")
public class CompCurrMB extends AbstractMB implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	private CompCurrRemote compBean;
	private CompCurr compCurr;
	private List<CompCurr> compCurrs;
	private CompCurr selectedCompCurr;

	private static Logger logger = Logger.getLogger(CompCurr.class.getName());
	private boolean skip;
	
	public CompCurrMB(){
		compCurr = new CompCurr();
		selectedCompCurr = new CompCurr();
	}
	
	//============================= GETS SETS ======================================
	public CompCurr getCompCurr() {
		return compCurr;
	}

	public void setCompCurr(CompCurr compCurr) {
		this.compCurr = compCurr;
	}

	public List<CompCurr> getCompCurrs() {
		compCurrs = compBean.listar();
		return compCurrs;
	}

	public void setCompCurrs(List<CompCurr> compCurrs) {
		this.compCurrs = compCurrs;
	}

	public CompCurr getSelectedCompCurr() {
		return selectedCompCurr;
	}

	public void setSelectedCompCurr(CompCurr selectedCompCurr) {
		this.selectedCompCurr = selectedCompCurr;
	}

	public List<Usuario> getProfessores(){
		return compBean.listarProfessores();
	}
	
	public TipoCompCurr[] getTipos(){
		return TipoCompCurr.values();
	}
	
	public FormaParticipacao[] getFormas(){
		return FormaParticipacao.values();
	}

	public boolean isSkip() {  
	        return skip;  
	}  
	  
	public void setSkip(boolean skip) {  
	        this.skip = skip;  
	}  
	
	//=================================================================================
	public String onFlowProcess(FlowEvent event) {  
	   logger.info("Current wizard step:" + event.getOldStep());  
	        logger.info("Next step:" + event.getNewStep());  
	          
	        if(skip) {  
	            skip = false;   //reset in case user goes back  
	            return "confirm";  
	        }  
	        else {  
	            return event.getNewStep();  
	        }  
	}  
	
	public void doInserir(ActionEvent actionEvent) throws Exception {  
		try {
			compBean.salvar(compCurr);
			compCurr = new CompCurr();
			RequestContext.getCurrentInstance().execute("dlgNovo.hide()");
		} catch (Exception e) {
			displayErrorMessageToUser("Erro ao salvar Componente Curricular.");
		}
    }
	
	public void doAlterar(ActionEvent actionEvent) throws Exception {  
		try {
			compBean.salvar(selectedCompCurr);	
			RequestContext.getCurrentInstance().execute("dlgEdit.hide()");
		} catch (Exception e) {
			displayErrorMessageToUser("Erro ao alterar Componente Curricular.");
		}
    }
	
	public void doRemover(CompCurr compCurr) {  
		try {
			compBean.excluir(compCurr.getId());	
		} catch (Exception e) {
			displayErrorMessageToUser("Erro ao excluir Componente Curricular.");
		}
    }
}
